package pl.edu.agh.uhm.serveragent;

import java.util.ArrayList;
import java.util.Dictionary;
import java.util.List;
import java.util.Properties;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.upnp.UPnPDevice;

import pl.edu.agh.uhm.serveragent.devices.vm.VMDevice;
import pl.edu.agh.uhm.xenconnector.BasicConnector;


public final class Activator implements BundleActivator{


	BasicConnector basicConnector;
	
    public void start( BundleContext bc )throws Exception{
    	
        System.out.println( "STARTING pl.edu.agh.uhm.serveragent" );
        basicConnector=new BasicConnector();
        List<String> vmUUIDs=basicConnector.getVmNames();   
        System.out.println("SERVERAGENT -- creating upnp device representing hypervisor");         
        HypervisorDevice hd=new HypervisorDevice(bc,BasicConnector.CONNECTION_URL,vmUUIDs);
        List<VMDevice> createdVMs=new ArrayList<VMDevice>();
        if(vmUUIDs!=null){
	        System.out.println("SERVERAGENT -- Following virual machines found:");
	        for(String s:vmUUIDs){   	
	        	System.out.println(s);
	        }
	        System.out.println("SERVERAGENT -- creating upnp devices representing virtual machines");
	        for(String s:vmUUIDs){   	
	        	VMDevice vm=new VMDevice(bc,s,hd.getDEVICE_ID());
	        	createdVMs.add(vm);     
	        	
	        }   
	        
        }
        else{
        	  System.out.println("SERVERAGENT -- no virual machines found:");	
        }
    
      	
        Dictionary hdDict=hd.getDescriptions(null);     	
        ServiceRegistration hdSR=bc.registerService(UPnPDevice.class.getName(), hd,hdDict);
        for(VMDevice vm:createdVMs){   
        	Dictionary vmDict=vm.getDescriptions(null);     	     
        	ServiceRegistration vmSR=bc.registerService(UPnPDevice.class.getName(), vm,vmDict);
        }
        	
     /*   
    	Dictionary dict1=hd1.getDescriptions(null);
    	Dictionary dict2=hd2.getDescriptions(null);
    	System.out.println("REGISTERING Hypervisor device");
    	ServiceRegistration serviceRegistration1=bc.registerService(UPnPDevice.class.getName(), hd1,
				dict1);
    	System.out.println("REGISTERING Hypervisor device");
    	
    	
    	ServiceRegistration serviceRegistration2=bc.registerService(UPnPDevice.class.getName(), hd2,
				dict2);
				
			*/	
    }

 
    public void stop( BundleContext bc )throws Exception{
        System.out.println( "STOPPING pl.edu.agh.uhm.serveragent" );

    }
}

